<?php

class Event {

    function addEvent() {
      //echo out 

    echo '<form method="POST" action="addEventHandler.php">' . "\n";

    echo "<fieldset><legend>Please fill out the following information to add an event.</legend>\n";

    echo '<label for="EventID">EventID:</label><input type="text" size="3" name="EventID" /><br /><br />' . "\n";
    echo '<label for="EventName">Name:</label><input type="text" size="50" name="EventName" /><br /><br />' . "\n";
    echo '<label for="Price">Price:</label><input type="text" size="10" name="Price" /><br /><br />' . "\n";
    echo '<label for="Date">Date:</label><input type="text" size="10" name="Date" /><br /><br />' . "\n";
    echo '<label for="Time">Time:</label><input type="text" size="10" name="Time" /><br /><br />' . "\n";

    $dbConn = odbc_connect('EventDB','MyUser','Temp123');

    if (!$dbConn) {
      exit("Connection Failed: " . $dbConn);
    }
    	
    $sql = "SELECT VenueID, VenueName FROM VENUE;";

    $rs = odbc_exec($dbConn,$sql);

    if (!$rs) {
      exit("Error in SQL");
    }

    echo '<label for="VenueID">Venue:</label><select name="VenueID">' . "\n";

    while(odbc_fetch_row($rs)) {
      $VenueID  = odbc_result($rs,"VenueID"); 
      $VenueName = odbc_result($rs, "VenueName");
                  
      echo '<option value="' . $VenueID . '">' . $VenueName . "</option>\n";
    }   
      
    echo "</select>\n";  

    echo "<br /><br />\n";

    $sql = "SELECT Ad_ID FROM Advertising;";

    $rs = odbc_exec($dbConn, $sql);

    echo '<label for="Ad_ID">Advertising ID:</label><select name="Ad_ID">' . "\n";

    while(odbc_fetch_row($rs)) {
      $Ad_ID  = odbc_result($rs,"Ad_ID"); 
                  
      echo '<option value="' . $Ad_ID . '">' . $Ad_ID . "</option>\n";
    }   
      
    echo "</select>\n";  

    echo "<br /><br />\n";

    $sql = "SELECT SponsorID, SponsorName FROM Sponsors;";

    $rs = odbc_exec($dbConn, $sql);

    echo '<label for="SponsorID">Sponsor Name:</label><select name="SponsorID">' . "\n";

    while(odbc_fetch_row($rs)) {
      $SponsorID  = odbc_result($rs,"SponsorID");
      $SponsorName = odbc_result($rs, "SponsorName");
                  
      echo '<option value="' . $SponsorID . '">' . $SponsorName . "</option>\n";
    }   
      
    echo "</select><br /><br />\n"; 

    echo '<input class="buttonS" type="submit" value="Submit" name="B1" /><input class="buttonR" type="reset" value="Reset" name="B2" /></fieldset></form>';

    odbc_free_result($rs);

    odbc_close($dbConn);

    }

    function addEventHandler() {

        $EventID=$_POST['EventID'];
        $EventName=$_POST['EventName'];
        $Price=$_POST['Price'];
        $Date=$_POST['Date'];
        $Time=$_POST['Time'];
        $VenueID=$_POST['VenueID'];
        $Ad_ID=$_POST['Ad_ID'];
        $SponsorID=$_POST['SponsorID'];

        $dbConn = odbc_connect("EventDB","MyUser","Temp123");
        if (!$dbConn)
          {
          die('Could not connect: ' . odbc_error());
          }
          
        $sql = "INSERT INTO Events VALUES($EventID, '$EventName', $Price, $Date, $Ad_ID, $SponsorID, $VenueID, $Time)";

        $rs = odbc_exec($dbConn, $sql);

        odbc_close($dbConn);

        echo "You entered the following Event Information<br /><br />";

        echo '<span class="label">EventID:</span>' . $EventID . "<br />\n";
        echo '<span class="label">Name:</span>' . $EventName . "<br />\n";
        echo '<span class="label">Price:</span>' . $Price . "<br />\n";
        echo '<span class="label">Date:</span>' . $Date . "<br />\n";
        echo '<span class="label">Time:</span>' . $Time . "<br />\n";

        $sql = "SELECT VenueName FROM Venue WHERE VenueID = $VenueID";

        $rs = odbc_exec($dbConn, $sql);

        while(odbc_fetch_row($rs))
        {
          $VenueName = odbc_result($rs, "VenueName");
          
          echo '<span class="label">Venue Name (ID):</span>' . $VenueName . " ($VenueID)<br />\n";
        }

        $sql = "SELECT SponsorName FROM Sponsors WHERE SponsorID = $SponsorID;";

        $rs = odbc_exec($dbConn, $sql);

        while(odbc_fetch_row($rs))
        {
          $SponsorName = odbc_result($rs, "SponsorName");
          
          echo '<span class="label">Sponsor Name (ID):</span>' . $SponsorName . " ($SponsorID)<br />\n";
        }

        echo '<span class="label">Advertising ID:</span>' . $Ad_ID . "<br />\n";

        odbc_free_result($rs);

        odbc_close($dbConn);
    }
}

?>